(function(){
    function AlertController(){
        this.AlertUI = {};
        this.TipUI = null;
        this.HomeLoaded = false;
        this.RoomLoaded = false;
        
        viewer.register("arena.resize", this.resize.bind(this));
        
        viewer.register("home.loaded", this.initHomeAlert.bind(this));
        viewer.register("room.loaded", this.initRoomAlert.bind(this));

        viewer.register("alert.show", this.show.bind(this));
        viewer.register("alert.close", this.close.bind(this));
        viewer.register("alert.tip", this.tip.bind(this));
        viewer.register("alert.tip.close", this.closeTip.bind(this));
    }

    var _proto = AlertController.prototype;

    /**
     * @public
     * 接收home.loaded消息，初始化大厅用到的弹窗资源
     */
    _proto.initHomeAlert = function () {
        if(this.HomeLoaded){return;}
        this.HomeLoaded = true;

        this.AlertUI["cashout"] = new ui.Alert.Cashout();
        this.AlertUI["rank"] = new ui.Alert.Rank();
        this.AlertUI["recharge"] = new ui.Alert.Recharge();
        this.AlertUI["normal"] = new ui.Alert.Normal();
        this.AlertUI["faq"] = new ui.Alert.Faq();
    }
    /**
     * @public
     * 接收room.loaded消息，初始化游戏房间用到的弹窗资源
     */
    _proto.initRoomAlert = function () {
        if(this.RoomLoaded){return;}
        this.RoomLoaded = true;
        
        this.AlertUI["chat"] = new ui.Alert.Chat();
        this.AlertUI["props"] = new ui.Alert.Props();
        this.AlertUI["normal"] = new ui.Alert.Normal();
    }
    /**
     * @public
     * 接收alert.show消息，显示弹窗，可携带参数
     * 
     * 对应弹窗需公开display()方法用于显示弹窗
     */
    _proto.show = function (module, params) {
        this.AlertUI[module].display(params);
    }
    /**
     * @public
     * 接收alert.close消息，关闭弹窗
     * 对应弹窗需公开close()方法用于关闭弹窗
     * 
     * @param {String} module 弹窗模块名，没有模块名关闭所有弹窗，包括气泡提示
     */
    _proto.close = function (module) {
        if(module){
            this.AlertUI[module].close();
        }else{
            for(var item in this.AlertUI){
                this.AlertUI[item].close();
            }

            this.closeTip();
        }
    }
    /**
     * @public
     * 接收alert.tip.close消息，关闭气泡提示
     */
    _proto.closeTip = function () {
        if(this.TipUI !== null){
            Laya.timer.clearAll(this);
            this.TipUI.removeSelf();
            this.TipUI = null;
        }
    }
    /**
     * @public
     * 接收alert.tip消息，显示气泡提示
     * 
     * @param {String} msg 气泡提示内容
     * @param {Number} timeout 气泡提示存在的时间
     * @param {Number} alpha 气泡提示的透明度，默认为1,取值0-1
     */
    _proto.tip = function (msg, timeout, alpha, scale) {
        this.closeTip();

        var tipUI = new ui.Alert.TipUI();
        tipUI.TipMessage.text = msg;

        tipUI.width = tipUI.TipMessage.width + 180;
        tipUI.height = tipUI.TipMessage.height + 50;
        tipUI.BG.alpha = alpha || 1;
        this.TipUI = tipUI;

        if(scale){
            tipUI.scale(scale[0], scale[1]);
        }

        Laya.stage.addChild(tipUI);
        Laya.timer.once(timeout, this, function () {
            this.TipUI.removeSelf();
            this.TipUI = null;
        });
    }
    /**
     * @public
     * 监听arena.resize消息，重置界面位置
     */
    _proto.resize = function () {
        if(!this.HomeLoaded && !this.RoomLoaded){return;}

        for(var i in this.AlertUI){
            var alert = this.AlertUI[i];
            
            if(alert.custom == true){
                alert.width = Laya.stage.width;
                alert.alertContent.centerX = 0;
            }
        }
    }

    new AlertController();
})();